﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using PAT.Entities;
using PAT.Data;

#endregion

namespace PAT.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="PatTipoIdentificacionProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class PatTipoIdentificacionProviderBaseCore : EntityProviderBase<PAT.Entities.PatTipoIdentificacion, PAT.Entities.PatTipoIdentificacionKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, PAT.Entities.PatTipoIdentificacionKey key)
		{
			return Delete(transactionManager, key.IdTipoIdentificacion);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_idTipoIdentificacion">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int16 _idTipoIdentificacion)
		{
			return Delete(null, _idTipoIdentificacion);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idTipoIdentificacion">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int16 _idTipoIdentificacion);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_TIPO_IDENTIFICACION_PAT_NATURALEZA_DOCUMENTO_IDENTIFICACION key.
		///		FK_PAT_TIPO_IDENTIFICACION_PAT_NATURALEZA_DOCUMENTO_IDENTIFICACION Description: 
		/// </summary>
		/// <param name="_idNaturaleza"></param>
		/// <returns>Returns a typed collection of PAT.Entities.PatTipoIdentificacion objects.</returns>
		public TList<PatTipoIdentificacion> GetByIdNaturaleza(System.Int16 _idNaturaleza)
		{
			int count = -1;
			return GetByIdNaturaleza(_idNaturaleza, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_TIPO_IDENTIFICACION_PAT_NATURALEZA_DOCUMENTO_IDENTIFICACION key.
		///		FK_PAT_TIPO_IDENTIFICACION_PAT_NATURALEZA_DOCUMENTO_IDENTIFICACION Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idNaturaleza"></param>
		/// <returns>Returns a typed collection of PAT.Entities.PatTipoIdentificacion objects.</returns>
		/// <remarks></remarks>
		public TList<PatTipoIdentificacion> GetByIdNaturaleza(TransactionManager transactionManager, System.Int16 _idNaturaleza)
		{
			int count = -1;
			return GetByIdNaturaleza(transactionManager, _idNaturaleza, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_TIPO_IDENTIFICACION_PAT_NATURALEZA_DOCUMENTO_IDENTIFICACION key.
		///		FK_PAT_TIPO_IDENTIFICACION_PAT_NATURALEZA_DOCUMENTO_IDENTIFICACION Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idNaturaleza"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT.Entities.PatTipoIdentificacion objects.</returns>
		public TList<PatTipoIdentificacion> GetByIdNaturaleza(TransactionManager transactionManager, System.Int16 _idNaturaleza, int start, int pageLength)
		{
			int count = -1;
			return GetByIdNaturaleza(transactionManager, _idNaturaleza, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_TIPO_IDENTIFICACION_PAT_NATURALEZA_DOCUMENTO_IDENTIFICACION key.
		///		fkPatTipoIdentificacionPatNaturalezaDocumentoIdentificacion Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_idNaturaleza"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT.Entities.PatTipoIdentificacion objects.</returns>
		public TList<PatTipoIdentificacion> GetByIdNaturaleza(System.Int16 _idNaturaleza, int start, int pageLength)
		{
			int count =  -1;
			return GetByIdNaturaleza(null, _idNaturaleza, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_TIPO_IDENTIFICACION_PAT_NATURALEZA_DOCUMENTO_IDENTIFICACION key.
		///		fkPatTipoIdentificacionPatNaturalezaDocumentoIdentificacion Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_idNaturaleza"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT.Entities.PatTipoIdentificacion objects.</returns>
		public TList<PatTipoIdentificacion> GetByIdNaturaleza(System.Int16 _idNaturaleza, int start, int pageLength,out int count)
		{
			return GetByIdNaturaleza(null, _idNaturaleza, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_TIPO_IDENTIFICACION_PAT_NATURALEZA_DOCUMENTO_IDENTIFICACION key.
		///		FK_PAT_TIPO_IDENTIFICACION_PAT_NATURALEZA_DOCUMENTO_IDENTIFICACION Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idNaturaleza"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of PAT.Entities.PatTipoIdentificacion objects.</returns>
		public abstract TList<PatTipoIdentificacion> GetByIdNaturaleza(TransactionManager transactionManager, System.Int16 _idNaturaleza, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override PAT.Entities.PatTipoIdentificacion Get(TransactionManager transactionManager, PAT.Entities.PatTipoIdentificacionKey key, int start, int pageLength)
		{
			return GetByIdTipoIdentificacion(transactionManager, key.IdTipoIdentificacion, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_PAT_TIPO_IDENTIFICACION index.
		/// </summary>
		/// <param name="_idTipoIdentificacion"></param>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatTipoIdentificacion"/> class.</returns>
		public PAT.Entities.PatTipoIdentificacion GetByIdTipoIdentificacion(System.Int16 _idTipoIdentificacion)
		{
			int count = -1;
			return GetByIdTipoIdentificacion(null,_idTipoIdentificacion, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_TIPO_IDENTIFICACION index.
		/// </summary>
		/// <param name="_idTipoIdentificacion"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatTipoIdentificacion"/> class.</returns>
		public PAT.Entities.PatTipoIdentificacion GetByIdTipoIdentificacion(System.Int16 _idTipoIdentificacion, int start, int pageLength)
		{
			int count = -1;
			return GetByIdTipoIdentificacion(null, _idTipoIdentificacion, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_TIPO_IDENTIFICACION index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idTipoIdentificacion"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatTipoIdentificacion"/> class.</returns>
		public PAT.Entities.PatTipoIdentificacion GetByIdTipoIdentificacion(TransactionManager transactionManager, System.Int16 _idTipoIdentificacion)
		{
			int count = -1;
			return GetByIdTipoIdentificacion(transactionManager, _idTipoIdentificacion, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_TIPO_IDENTIFICACION index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idTipoIdentificacion"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatTipoIdentificacion"/> class.</returns>
		public PAT.Entities.PatTipoIdentificacion GetByIdTipoIdentificacion(TransactionManager transactionManager, System.Int16 _idTipoIdentificacion, int start, int pageLength)
		{
			int count = -1;
			return GetByIdTipoIdentificacion(transactionManager, _idTipoIdentificacion, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_TIPO_IDENTIFICACION index.
		/// </summary>
		/// <param name="_idTipoIdentificacion"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatTipoIdentificacion"/> class.</returns>
		public PAT.Entities.PatTipoIdentificacion GetByIdTipoIdentificacion(System.Int16 _idTipoIdentificacion, int start, int pageLength, out int count)
		{
			return GetByIdTipoIdentificacion(null, _idTipoIdentificacion, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_TIPO_IDENTIFICACION index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idTipoIdentificacion"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatTipoIdentificacion"/> class.</returns>
		public abstract PAT.Entities.PatTipoIdentificacion GetByIdTipoIdentificacion(TransactionManager transactionManager, System.Int16 _idTipoIdentificacion, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;PatTipoIdentificacion&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;PatTipoIdentificacion&gt;"/></returns>
		public static TList<PatTipoIdentificacion> Fill(IDataReader reader, TList<PatTipoIdentificacion> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				PAT.Entities.PatTipoIdentificacion c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("PatTipoIdentificacion")
					.Append("|").Append((System.Int16)reader[((int)PatTipoIdentificacionColumn.IdTipoIdentificacion - 1)]).ToString();
					c = EntityManager.LocateOrCreate<PatTipoIdentificacion>(
					key.ToString(), // EntityTrackingKey
					"PatTipoIdentificacion",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new PAT.Entities.PatTipoIdentificacion();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.IdTipoIdentificacion = (System.Int16)reader[((int)PatTipoIdentificacionColumn.IdTipoIdentificacion - 1)];
					c.DescripcionTipoIdentificacion = (System.String)reader[((int)PatTipoIdentificacionColumn.DescripcionTipoIdentificacion - 1)];
					c.IdNaturaleza = (System.Int16)reader[((int)PatTipoIdentificacionColumn.IdNaturaleza - 1)];
					c.Activo = (System.Boolean)reader[((int)PatTipoIdentificacionColumn.Activo - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="PAT.Entities.PatTipoIdentificacion"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="PAT.Entities.PatTipoIdentificacion"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, PAT.Entities.PatTipoIdentificacion entity)
		{
			if (!reader.Read()) return;
			
			entity.IdTipoIdentificacion = (System.Int16)reader[((int)PatTipoIdentificacionColumn.IdTipoIdentificacion - 1)];
			entity.DescripcionTipoIdentificacion = (System.String)reader[((int)PatTipoIdentificacionColumn.DescripcionTipoIdentificacion - 1)];
			entity.IdNaturaleza = (System.Int16)reader[((int)PatTipoIdentificacionColumn.IdNaturaleza - 1)];
			entity.Activo = (System.Boolean)reader[((int)PatTipoIdentificacionColumn.Activo - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="PAT.Entities.PatTipoIdentificacion"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="PAT.Entities.PatTipoIdentificacion"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, PAT.Entities.PatTipoIdentificacion entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.IdTipoIdentificacion = (System.Int16)dataRow["ID_TIPO_IDENTIFICACION"];
			entity.DescripcionTipoIdentificacion = (System.String)dataRow["DESCRIPCION_TIPO_IDENTIFICACION"];
			entity.IdNaturaleza = (System.Int16)dataRow["ID_NATURALEZA"];
			entity.Activo = (System.Boolean)dataRow["ACTIVO"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="PAT.Entities.PatTipoIdentificacion"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">PAT.Entities.PatTipoIdentificacion Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, PAT.Entities.PatTipoIdentificacion entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region IdNaturalezaSource	
			if (CanDeepLoad(entity, "PatNaturalezaDocumentoIdentificacion|IdNaturalezaSource", deepLoadType, innerList) 
				&& entity.IdNaturalezaSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.IdNaturaleza;
				PatNaturalezaDocumentoIdentificacion tmpEntity = EntityManager.LocateEntity<PatNaturalezaDocumentoIdentificacion>(EntityLocator.ConstructKeyFromPkItems(typeof(PatNaturalezaDocumentoIdentificacion), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.IdNaturalezaSource = tmpEntity;
				else
					entity.IdNaturalezaSource = DataRepository.PatNaturalezaDocumentoIdentificacionProvider.GetByIdNaturaleza(transactionManager, entity.IdNaturaleza);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'IdNaturalezaSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.IdNaturalezaSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.PatNaturalezaDocumentoIdentificacionProvider.DeepLoad(transactionManager, entity.IdNaturalezaSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion IdNaturalezaSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByIdTipoIdentificacion methods when available
			
			#region PatUsuarioCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<PatUsuario>|PatUsuarioCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'PatUsuarioCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.PatUsuarioCollection = DataRepository.PatUsuarioProvider.GetByIdTipoIdentificacion(transactionManager, entity.IdTipoIdentificacion);

				if (deep && entity.PatUsuarioCollection.Count > 0)
				{
					deepHandles.Add("PatUsuarioCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<PatUsuario>) DataRepository.PatUsuarioProvider.DeepLoad,
						new object[] { transactionManager, entity.PatUsuarioCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the PAT.Entities.PatTipoIdentificacion object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">PAT.Entities.PatTipoIdentificacion instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">PAT.Entities.PatTipoIdentificacion Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, PAT.Entities.PatTipoIdentificacion entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region IdNaturalezaSource
			if (CanDeepSave(entity, "PatNaturalezaDocumentoIdentificacion|IdNaturalezaSource", deepSaveType, innerList) 
				&& entity.IdNaturalezaSource != null)
			{
				DataRepository.PatNaturalezaDocumentoIdentificacionProvider.Save(transactionManager, entity.IdNaturalezaSource);
				entity.IdNaturaleza = entity.IdNaturalezaSource.IdNaturaleza;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
	
			#region List<PatUsuario>
				if (CanDeepSave(entity.PatUsuarioCollection, "List<PatUsuario>|PatUsuarioCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(PatUsuario child in entity.PatUsuarioCollection)
					{
						if(child.IdTipoIdentificacionSource != null)
						{
							child.IdTipoIdentificacion = child.IdTipoIdentificacionSource.IdTipoIdentificacion;
						}
						else
						{
							child.IdTipoIdentificacion = entity.IdTipoIdentificacion;
						}

					}

					if (entity.PatUsuarioCollection.Count > 0 || entity.PatUsuarioCollection.DeletedItems.Count > 0)
					{
						//DataRepository.PatUsuarioProvider.Save(transactionManager, entity.PatUsuarioCollection);
						
						deepHandles.Add("PatUsuarioCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< PatUsuario >) DataRepository.PatUsuarioProvider.DeepSave,
							new object[] { transactionManager, entity.PatUsuarioCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region PatTipoIdentificacionChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>PAT.Entities.PatTipoIdentificacion</c>
	///</summary>
	public enum PatTipoIdentificacionChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>PatNaturalezaDocumentoIdentificacion</c> at IdNaturalezaSource
		///</summary>
		[ChildEntityType(typeof(PatNaturalezaDocumentoIdentificacion))]
		PatNaturalezaDocumentoIdentificacion,
	
		///<summary>
		/// Collection of <c>PatTipoIdentificacion</c> as OneToMany for PatUsuarioCollection
		///</summary>
		[ChildEntityType(typeof(TList<PatUsuario>))]
		PatUsuarioCollection,
	}
	
	#endregion PatTipoIdentificacionChildEntityTypes
	
	#region PatTipoIdentificacionFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;PatTipoIdentificacionColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatTipoIdentificacion"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatTipoIdentificacionFilterBuilder : SqlFilterBuilder<PatTipoIdentificacionColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatTipoIdentificacionFilterBuilder class.
		/// </summary>
		public PatTipoIdentificacionFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the PatTipoIdentificacionFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public PatTipoIdentificacionFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the PatTipoIdentificacionFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public PatTipoIdentificacionFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion PatTipoIdentificacionFilterBuilder
	
	#region PatTipoIdentificacionParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;PatTipoIdentificacionColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatTipoIdentificacion"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatTipoIdentificacionParameterBuilder : ParameterizedSqlFilterBuilder<PatTipoIdentificacionColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatTipoIdentificacionParameterBuilder class.
		/// </summary>
		public PatTipoIdentificacionParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the PatTipoIdentificacionParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public PatTipoIdentificacionParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the PatTipoIdentificacionParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public PatTipoIdentificacionParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion PatTipoIdentificacionParameterBuilder
	
	#region PatTipoIdentificacionSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;PatTipoIdentificacionColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatTipoIdentificacion"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class PatTipoIdentificacionSortBuilder : SqlSortBuilder<PatTipoIdentificacionColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatTipoIdentificacionSqlSortBuilder class.
		/// </summary>
		public PatTipoIdentificacionSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion PatTipoIdentificacionSortBuilder
	
} // end namespace
